package com.flyoil.petromp.utils.OrderUtl;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cnpc.fyviewlibrary.view.GridScrollview;
import com.cnpc.fyviewlibrary.view.ListScrollview;
import com.flyoil.petromp.R;
import com.flyoil.petromp.adapter.adapter_order.ItemImgOrderMessageAdapter;
import com.flyoil.petromp.adapter.adapter_order.ItemOrderMessageGoodsAdapter;
import com.flyoil.petromp.entity.entity_order.MapListEntity;
import com.flyoil.petromp.entity.entity_order.OrderFilesEntity;
import com.flyoil.petromp.ui.activity.activity_matter.OrderGoodsMessageActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/10/27.
 * 工单详情界面UI初始化的工具类
 */

public class OrderMessageUtil {
    private Context mContext;
    private String order_type = "";

    public OrderMessageUtil(Context mContext) {
        this.mContext = mContext;
    }

    public OrderMessageUtil(Context mContext, String order_type) {
        this.mContext = mContext;
        this.order_type = order_type;
    }

    /**
     * 初始化一组键值对的文本列表布局
     *
     * @param mapListEntities
     * @param line_layout
     */
    public void init_text_map_view(List<MapListEntity> mapListEntities, LinearLayout line_layout) {
        if (mapListEntities == null || mapListEntities.size() == 0 || line_layout == null) {
            return;
        }
        for (int i = 0; i < mapListEntities.size(); i++) {
            MapListEntity entity = mapListEntities.get(i);
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_message_text_layout, null);
            TextView tv_name = view.findViewById(R.id.tv_item_order_message_text_name);
            TextView tv_value = view.findViewById(R.id.tv_item_order_message_text_value);
            tv_name.setText(entity.getName());
            tv_value.setText(entity.getValue());
            line_layout.addView(view);
        }
    }

    /**
     * 初始化单据详情的标题部分
     *
     * @param imgRese
     * @param name
     * @param line_layout
     */
    public void init_message_title_layout_view(int imgRese, String name, LinearLayout line_layout) {
        if (name != null && line_layout != null && imgRese > 0) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_message_title_layout, null);
            ImageView img_icon = view.findViewById(R.id.img_item_order_message_title_icon);
            TextView tv_name = view.findViewById(R.id.tv_item_order_message_title_name);
            img_icon.setImageResource(imgRese);
            tv_name.setText(name);
            line_layout.addView(view);
        }
    }

    /**
     * 初始化备注的布局
     *
     * @param entity
     * @param line_layout
     */
    public void init_remark_view(MapListEntity entity, LinearLayout line_layout) {
        if (entity != null && line_layout != null) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_message_remark_layout, null);
            TextView tv_name = view.findViewById(R.id.tv_item_order_message_remark_name);
            TextView tv_value = view.findViewById(R.id.tv_item_order_message_remark_value);
            tv_name.setText(entity.getName());
            tv_value.setText(entity.getValue());
            line_layout.addView(view);
        }
    }

    /**
     * 初始化明细列表的布局
     */
    public void init_order_message_goods_view(List data, LinearLayout line_layout) {
        if (data != null && data.size() > 0 && line_layout != null) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_message_goods_services_layout, null);
            ListScrollview list_goods = view.findViewById(R.id.list_item_order_message_goods_services);
            final ItemOrderMessageGoodsAdapter adapter_goods = new ItemOrderMessageGoodsAdapter(mContext);
            list_goods.setAdapter(adapter_goods);
            list_goods.setDividerHeight(0);
            adapter_goods.setdate(data);
            list_goods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //服务验收单详情界面中的服务明细没有的点击事件
                    if (OrderListUtil.SERVICE_EXAMINE.equals(order_type)) {
                        return;
                    }
                    Intent intent = new Intent(mContext, OrderGoodsMessageActivity.class);
                    intent.putExtra("order_type", order_type);
                    intent.putExtra("id", adapter_goods.getData().get(position).getId());
                    mContext.startActivity(intent);
                }
            });
            line_layout.addView(view);
        }
    }

    /**
     * 初始化图片列表的布局
     *
     * @param line_layout
     */
    public void init_imgs_view(String name, List<OrderFilesEntity> imgUrl, LinearLayout line_layout) {
        if (imgUrl != null && line_layout != null) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_message_photos_layout, null);
            TextView tv_name = view.findViewById(R.id.tv_item_order_message_photos_name);
            GridScrollview gridScrollview = view.findViewById(R.id.grid_item_order_message_photos_layout);
            tv_name.setText(name);
            ItemImgOrderMessageAdapter adapter = new ItemImgOrderMessageAdapter(mContext);
            gridScrollview.setAdapter(adapter);
            adapter.addDataBottom(imgUrl);
            line_layout.addView(view);
        }
    }


    /**
     * 获取单据在不同情况下的标题
     *
     * @param type
     * @return
     */
    public String getOrderMessageTitle(String type) {
        String title = "单据";
        switch (type) {
            case OrderListUtil.PLAN:
                title = "采购计划单";
                break;
            case OrderListUtil.GOODS_DEMAND:
                title = "物资需求单";
                break;
            case OrderListUtil.GOODS_APPROVE:
                title = "物资采购审批表";
                break;
            case OrderListUtil.GOODS_ORDER:
                title = "物资采购订单";
                break;
            case OrderListUtil.GOODS_EXAMINE:
                title = "物资验收单";
                break;
            case OrderListUtil.GOODS_DATE_EXAMINE:
                title = "质保期验收单详情";
                break;
            case OrderListUtil.SERVICE_DEMAND:
                title = "服务任务需求";
                break;
            case OrderListUtil.SERVICE_APPROVE:
                title = "服务任务审批表";
                break;
            case OrderListUtil.SERVICE_ORDER:
                title = "服务任务订单";
                break;
            case OrderListUtil.SERVICE_EXAMINE:
                title = "服务任务验收单";
                break;
            case OrderListUtil.FINANCE:
                title = "付款申请单";
                break;
        }
        return title;
    }

    /**
     * 获取单据明细详情信息的标题
     *
     * @param type
     * @return
     */
    public String getOrderGoodsMessageTitle(String type) {
        String title = "明细";
        switch (type) {
            case OrderListUtil.PLAN:
                title = "采购计划明细";
                break;
            case "物资采购计划单服务明细":
                title = "服务任务需求明细";
                break;
            case OrderListUtil.GOODS_DEMAND:
                title = "物资需求明细";
                break;
            case OrderListUtil.GOODS_APPROVE:
                title = "物资采购审批明细";
                break;
            case OrderListUtil.GOODS_ORDER:
                title = "物资采购订单明细";
                break;
            case OrderListUtil.GOODS_EXAMINE:
                title = "物资验收明细";
                break;
            case OrderListUtil.GOODS_DATE_EXAMINE:
                title = "质保期验收单详情";
                break;
            case OrderListUtil.SERVICE_DEMAND:
                title = "服务任务需求明细";
                break;
            case OrderListUtil.SERVICE_APPROVE:
                title = "服务任务审批明细";
                break;
            case OrderListUtil.SERVICE_ORDER:
                title = "服务任务订单明细";
                break;
            case OrderListUtil.SERVICE_EXAMINE:
                title = "服务任务验收明细";
                break;
            case OrderListUtil.FINANCE:
                title = "付款申请明细";
                break;
        }
        return title;
    }

    /**
     * 将供应商集合初始化到布局
     *
     * @param entitys
     * @param line_layout
     */
    public void init_suppliers_view(List<String> entitys, LinearLayout line_layout) {
        if (entitys == null || entitys.size() == 0 || line_layout == null) {
            return;
        }
        for (int i = 0; i < entitys.size(); i++) {
            String name = entitys.get(i);
            TextView tv_supplier = new TextView(mContext);
            tv_supplier.setText(name);
            tv_supplier.setTextSize(14);
            tv_supplier.setTextColor(mContext.getResources().getColor(R.color.black));
            tv_supplier.setPadding(0, 2, 0, 2);
            line_layout.addView(tv_supplier);
        }
    }
}
